#pragma once
#include <fstream>
#include <string>
#include <ostream>
#include <INetworkSnapshotProvider.h>
#include <XMLNetworkSnapshotParser.h>
#include <ViewerException.h>

struct NetworkSnapshotProviderFile: INetworkSnapshotProvider {
  NetworkSnapshotProviderFile(const std::string fileName) 
    : fileName(fileName)
  {}
  virtual NetworkSnapshotPtr getSnapshot(std::ostream& errs) {
    using namespace rapidxml;
    NetworkSnapshotPtr snapshot(new NetworkSnapshot());
    
    std::ifstream in(fileName.c_str());
    if (!in.good()) {
      throw ViewerException("Bad input file " + fileName);
    }
    std::string inputXML;
    std::string line;

    while(getline(in, line)) {
      inputXML += line;
    }
    
    std::vector<char> xmlCopy(inputXML.begin(), inputXML.end());
    xmlCopy.push_back('\0');
    xml_document<> doc;
    doc.parse<parse_declaration_node | parse_no_data_nodes>(&xmlCopy[0]);

    XMLNetworkSnapshotParser::parse(doc, snapshot, errs);
    in.close();
    return snapshot;
  }
private:
  std::string fileName;
};
